package com.livew.framework.utils;

import java.util.Random;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;

public class XTStringUtils extends StringUtils{
	
	
	/**
	 * 生成随机数 
	 * @param length 为null或者length小于1 则采用默认的长度 6
	 * @return
	 */
	public static String getRandom(Integer length) {
		Random random = new Random();
		if (length == null || length < 1) {
			length = 6;
		}
		String result = "";
		for (int i = 0; i < length; i++) {
			result += random.nextInt(10);
		}
		return result;
	}
	
	
	public static boolean isNumeric(String str) {
		for (int i = 0; i < str.length(); i++) {
			if (!Character.isDigit(str.charAt(i))) {
				return false;
			}
		}
		return true;
	}

	
	/**
	 * 随机字符串
	 * @param length
	 * @return
	 */
	public static String captcha(int length) {
		String VERIFY_CODES = "12356789abcdeghjkmnpqrtuvwxyABCDEGHJKLMNPQRTUVWXY";
		int codesLen = VERIFY_CODES.length();
		Random rand = new Random(System.currentTimeMillis());
		StringBuilder verifyCode = new StringBuilder(length);
		for (int i = 0; i < length; i++) {
			verifyCode.append(VERIFY_CODES.charAt(rand.nextInt(codesLen - 1)));
		}
		return verifyCode.toString();
	}
	
	/**
	 * 随机字符串
	 * @param length
	 * @return
	 */
	public static String getUserName(int length) {
		String VERIFY_CODES = "abcdeghjkmnpqrtuvwxyabcdeghjklmnpqrtuvwxy";
		int codesLen = VERIFY_CODES.length();
		Random rand = new Random(System.currentTimeMillis());
		StringBuilder verifyCode = new StringBuilder(length);
		for (int i = 0; i < length; i++) {
			verifyCode.append(VERIFY_CODES.charAt(rand.nextInt(codesLen - 1)));
		}
		return verifyCode.toString();
	}
	
	/**
	 * 获取uuid
	 * @return
	 */
	public static String getUUID(){
		String uuid = UUID.randomUUID().toString().replace("-", "");
		return uuid;
	}
	
	
	
	/**
	 * 取文件后缀名
	 * @param name
	 * @return
	 */
	public static String getPrefix(String name){
		String prefix = "";
		if(!StringUtils.isEmpty(name)&&name.contains(".")){
			prefix = "."+name.substring(name.lastIndexOf(".")+1);
		}
		return prefix;
	}
	
}
